import { mapState } from 'vuex'
import { userModule } from '@/store/modules/user'

import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { subsidyServices } from '@/common/services/subsidy'
import { TableMixin } from '@/mixin'
import { cropMemberServices } from '@/common/services/crop-member'
import { Modal } from 'ant-design-vue'

@Component
export class RosterMixins extends Mixins(TableMixin) {
  @Prop({
    type: String
  })
  public currentAreaId!: string

  public isShowIndexColumn = true

  public isUsed = 0
  public batchIsUsed = false

  public batchIsUsedLoading = false;

  public async getData() {
    this.isInitSelected = false
    let extra: Record<string, string> = {}
    // 查错条件格式化
    if (this.tableSearch.member_filter?.length) {
      this.tableSearch.member_filter.forEach((key: string) => {
        extra[key] = '1'
      })
    }
    const res = await this.getTableList(extra)
    this.selectedRowKeys = this.tableData
      .filter(item => +item.is_used === 1)
      .map(item => item.member_id)

    // 已选择用户数据
    const totalData = this.tableData[0]
    if (totalData && totalData.is_used_num) {
      this.isUsed = totalData.is_used_num
    }

    // 大户重写时间相关字段
    this.tableData = this.tableData.map(item => {
      if (item.apl_time) {
        item.apl_time = this.removeTime(item.apl_time)
      }
      if (item.big_mb_time) {
        item.big_mb_time = this.removeTime(item.big_mb_time)
      }
      if (item.big_mb_time1) {
        item.big_mb_time1 = this.removeTime(item.big_mb_time1)
      }
      if (item.big_mb_time2) {
        item.big_mb_time2 = this.removeTime(item.big_mb_time2)
      }
      if (item.big_mb_time3) {
        item.big_mb_time3 = this.removeTime(item.big_mb_time3)
      }
      return item
    })

    // 未设置补贴金额提示
    const currentPrice = totalData ? totalData.pay_price : 0
    // if (totalData && (!currentPrice || currentPrice <= 0)) {
    //   this.$warning({
    //     content: '该区县未设置补贴标准，无法计算补贴金额！请联系该区县级管理员尽快设置...',
    //     centered: true
    //   })
    // }
    this.$nextTick(() => {
      this.isInitSelected = true
    })
  }

  @Watch('batchIsUsed')
  public handlerChangeBatchIsUsed(val: boolean) {
    this.batchIsUsedLoading = true
    cropMemberServices
      .setCropMemberUsed({
        is_area: this.currentAreaId,
        is_used: val ? '1' : '2'
      })
      .then(() => {
        this.getData()
        Modal.success({
          content: '补贴状态更新成功',
          centered: true
        })
      }).finally(() => {
        this.batchIsUsedLoading = false
      })
  }

  public isInitSelected = false
  @Watch('selectedRowKeys')
  public handlerChangeSelectedRowKeys(newVals: string[], oldVals: string[]) {
    if (!this.isInitSelected) return
    let isUsed = 1
    let ids: string[] = []
    if (newVals.length - oldVals.length > 0) {
      // 添加
      isUsed = 1
      ids = newVals.filter(str => !oldVals.includes(str))
    } else {
      // 移除
      isUsed = 2
      ids = oldVals.filter(str => !newVals.includes(str))
    }
    if (!ids.length) return
    const params: Record<string, string> = {
      member_id: ids.join(',')
    }
    // 单次修改添加s_a_id
    if (ids.length === 1) {
      const currentRow = this.tableData.find(item => ids.includes(item.member_id))
      currentRow && (params.s_a_id = currentRow.s_a_id)
    }
    cropMemberServices
      .setCropMemberUsed({
        ...params,
        is_used: `${isUsed}`
      })
      .then(() => {
        this.getData()
        Modal.success({
          content: '补贴状态更新成功',
          centered: true
        })
      })
  }

  public handlerTableExportExecl(isSupportSort = false) {
    let extra: Record<string, string> = {}
    // 查错条件格式化
    if (this.tableSearch.member_filter?.length) {
      this.tableSearch.member_filter.forEach((key: string) => {
        extra[key] = '1'
      })
    }
    this.tableExportExecl(extra, false, {
      title: isSupportSort ? '设置导出Execl电子表的字段及顺序' : '',
      isSupportSort
    })
  }

  /**
   * 移除时间
   *
   * @param {string} datetime
   * @returns
   * @memberof RosterMixins
   */
  public removeTime(datetime: string) {
    return datetime ? datetime.split(' ')[0] : ''
  }
}
